#ifndef CAPTHREADV4L2_H
#define CAPTHREADV4L2_H


#include "img_conv.h"
#include <QImage>




/* 摄像头像素格式及其描述信息结构体 */
typedef struct camera_format {
    unsigned char description[32];  //字符串描述信息
    unsigned int pixelformat;       //像素格式
} cam_fmt;

/* buffer描述信息结构体 */
struct buffer_info {
    unsigned char   *start;
    size_t offset;
    int    filled;
    int    invalid;
    unsigned int length;
};


typedef struct
{
    int                             nChannel;

    /* v4l2结构体 */
    struct v4l2_format              fmt;
    struct v4l2_buffer              buf,one_buf;
    struct v4l2_requestbuffers      reqbuf;
    struct v4l2_capability          cap;
    struct v4l2_fmtdesc             fmtdesc;
    struct v4l2_frmsizeenum         frmsize;
    struct v4l2_frmivalenum         frmival;
    enum v4l2_buf_type              buff_type;
    struct timeval                  tv;
    int                             v4l2_fd;    //返回值ret和v4l2描述符v4l2_fd
    unsigned int                    n_buffers;

    cam_fmt                         cam_fmts[64];
    struct buffer_info *            buffer_infos;
    FILE*                           fOut;

    int                             nCapWidth;
    int                             nCapHeight;

    char                            cChnName[STR_BUFF_LEN];
    int                             m_nSeq; //seq number ,cal lost



    int                             m_YUVWidth;
    int                             m_YUVHeight;

    int                             m_nAlgWidth;
    int                             m_nAlgHeight;
    stVFrame*                       m_pFrame[MAX_FRAME_BUFF_CNT];
    int                             m_nWritePos;

    float                           m_fMinScale;

    int                             m_I2cFd;

    uint8_t*                        m_pTmpFrame; //只有红外有奇偶场问题，暂时只对红外视频有效
    int                             m_nHalfFrameCnt;
}stChnV4L2;



#include <QWidget>
#include <QThread>
#include <QStringList>



class CapThreadV4L2: public QThread
{
    Q_OBJECT
public:
    explicit  CapThreadV4L2(QObject *parent);
    ~CapThreadV4L2();


    void    run()  override;
    void    Stop();

    void    SetChannel(int  chn);


    QString  GetCapInfo(int  chn);


    int     GetFps_tv() {int nRes = m_nFps_tv; m_nFps_tv = 0; return nRes; }
    int     GetFps_ir() {int nRes = m_nFps_ir; m_nFps_ir = 0; return nRes; }

    void    SetVideoCtrl(uint8_t  newVideoCtrl) {ucVideoCtrl2 = newVideoCtrl;}

signals:
     void   AlgProcFrame(stVFrame* newFrame);
     void   IRVideoFrame(uint8_t* pImgData,int nLength,int nErrCode);

private:
    QObject*            m_parent;
    //QThreadPool         m_varThreadPool;
    volatile int        m_ThreadRun;



    stChnV4L2*          m_pChnCfg;


    uint8_t*            m_pTmpFrame;
    uint8_t*            m_pScaleYUV;


    volatile int        m_nFps_tv;
    volatile int        m_nFps_ir;
    volatile int        m_nFrameCnt;

    int                 m_nVisibleWidth;
    int                 m_nVisibleHeight;

    int                 m_nInfraredWidth;
    int                 m_nInfraredHeight;

    int                 m_nOutWidth;
    int                 m_nOutHeight;

   volatile  uint8_t             ucVideoCtrl2;

    int                 SelectGetFrame();

    void                CapInitAlgV4l2(int nChn);
    bool                ldGetChnFrame(int nChannel,stVFrame* pNewFrame,int nWidth,int nHeight);

};


#endif // CAPTHREADV4L2_H
